use tauri_plugin_http::reqwest;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    HTTP(#[from] reqwest::Error),
}

impl serde::Serialize for Error {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.to_string().as_ref())
    }
}

/**
* This function get metar info from vatsim service center
*/
#[tauri::command]
pub async fn get_metar(airport: &str) -> Result<String, Error> {
    if airport.is_empty() {
        return Ok("".to_string());
    }
    let res = reqwest::get(format!("https://metar.vatsim.net/{}", airport))
        .await?
        .text()
        .await?;
    Ok(res)
}
